

package cn.shengchao.examstar.pay.model.service;

import cn.shengchao.examstar.pay.acl.ports.client.IProductClient;
import cn.shengchao.examstar.pay.acl.ports.repositories.IOrderRepository;
import cn.shengchao.examstar.pay.infrastructure.dao.po.PayOrder;
import cn.shengchao.examstar.pay.model.aggregate.CreateOrderAggregate;
import cn.shengchao.examstar.pay.model.entity.PayOrderEntity;
import cn.shengchao.examstar.pay.model.valobj.OrderStatusVO;
import cn.shengchao.examstar.pay.utils.pay.PayContext;
import cn.shengchao.examstar.pay.utils.pay.PayTypeBase;
import cn.shengchao.examstar.pay.utils.pay.PayTypeEnum;
import cn.shengchao.examstar.pay.utils.pay.UIPayResponse;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class OrderService extends AbstractOrderService {

    public OrderService(IOrderRepository repository, IProductClient productPort) {
        super(repository, productPort);
    }

    @Override
    protected void doSaveOrder(CreateOrderAggregate orderAggregate) {
        repository.doSaveOrder(orderAggregate);
    }

    @Override
    protected PayOrderEntity doPrepayOrder(String userId,
                                           String openId,
                                           String appid,
                                           String productId,
                                           String productName,
                                           String orderId,
                                           String ip,
                                           String type,
                                           BigDecimal totalAmount) throws Exception {
        //调用支付
        PayTypeBase payTypeBase = PayTypeEnum.getByCode(type).getPayType();
        UIPayResponse<WxPayMpOrderResult> res = payTypeBase.createPayOrder(PayContext.builder()
                .price(totalAmount.multiply(BigDecimal.valueOf(100)).intValue())
                .ip(ip)
                .openId(openId)
                .appid(appid)
                .body(productName)
                .orderId(orderId)
                .build());
        PayOrderEntity payOrderEntity = new PayOrderEntity();
        payOrderEntity.setOrderId(orderId);
        payOrderEntity.setPayInfo(res.getPayParam());
        payOrderEntity.setOrderStatus(OrderStatusVO.PAY_WAIT);
        // 更新订单支付信息
        repository.updateOrderPayInfo(payOrderEntity);
        return payOrderEntity;
    }

    @Override
    public void changeOrderPaySuccess(String orderId) {
        repository.changeOrderPaySuccess(orderId);
    }

    @Override
    public List<PayOrder> queryNoPayNotifyOrder() {
        return repository.queryNoPayNotifyOrder();
    }

    @Override
    public List<String> queryTimeoutCloseOrderList() {
        return repository.queryTimeoutCloseOrderList();
    }

    @Override
    public boolean changeOrderClose(String orderId) {
        return repository.changeOrderClose(orderId);
    }

}
